<template>
  <!-- 不合格标本记录子组件 -->
  <div id="unqSpecRec">
    <Splitpanes horizontal>
      <Pane size="50">
        <div>
          <vxe-toolbar perfect>
            <template #buttons>
              <span class="padding-lr-xs">
                <vxe-button type="text" content="新增" class-name="padding-lr-xs" class="button-blue padding-lr-xs" @click="saveUnqualified" />
              </span>
              <span class="padding-lr-xs">
                <vxe-button type="text" content="保存" class-name="padding-lr-xs" class="button-blue padding-lr-xs" @click="save" />
              </span>
              <span class="padding-lr-xs">
                <vxe-button type="text" content="删除" class-name="padding-lr-xs" class="button-red padding-lr-xs" @click="handleDelete(currentUnqSpecRec.id)" />
              </span>
            </template>
          </vxe-toolbar>
        </div>
        <div class="unqApplicationRecTable" style="height: calc(100% - 30px)">
          <vxe-table ref="unqApplicationRecTable" v-loading="loading" border height="100%" :data="unqApplicationRecList" @current-change="unqApplicationRecRowClick" :row-config="{ isCurrent: true, isHover: true }" :keyboard-config="{
              isArrow: true,
            }" resizable show-overflow>
            <vxe-column type="seq" title="序号" width="30"></vxe-column>
            <vxe-column field="operatorTime" title="登记时间" width="120">
            </vxe-column>
            <vxe-column field="illustrate" title="不合格标本原因" show-overflow-tooltip>
            </vxe-column>
            <vxe-column field="handler" title="处理人" width="80">
            </vxe-column>
            <vxe-column field="handleInfo" title="处理意见" show-overflow-tooltip>
            </vxe-column>
            <vxe-column field="handleTime" title="处理时间" width="120">
            </vxe-column>
          </vxe-table>
        </div>
      </Pane>
      <Pane size="50">
        <UnqualifiedSpecRec ref="unqualifiedSpecRecRef" @saveReasons="saveReasons" />
      </Pane>
    </Splitpanes>

  </div>
</template>
<script lang="ts" setup>
import { ref, reactive, getCurrentInstance, onMounted, ObjectDirective } from 'vue';
import { VxeMessage, VxeNotifyType } from '../utils/CommonMessage';
import { IsEmpty, formatDateTime, TimePickerOpts } from '@/utils/common';
import { Splitpanes, Pane } from 'splitpanes';
import UnqualifiedSpecRec from '@/components/UnqualifiedSpecRec.vue';

import { AppStore } from '../stores/store';

let { proxy }: any = getCurrentInstance();
let store = AppStore();
// 方法定义区 =============
defineExpose({ getSample });

let loading = ref(false);
let unqualifiedSpecRecRef: any = ref(null); //复查
let currentUnqSpecRec = ref({}) as any; //当前选中行
let oldChackedReason = reactive([]); //列表改变前的不合格原因
let chackedReasonIds = reactive([]); //选中的不合格原因
let unqApplicationRecList = reactive([]); //列表
let currentRowIndex = 0;
let Rloading = ref(false);
let form = reactive({
  id: '',
  reqMainId: '',
  reqNo: '',
  patTypeNa: '',
  patNa: '',
  sex: '',
  age: '',
  sampleType: '',
  testPurpose: '',
  rptGroup: '',
  sampleda: '',
  sampleNo: '',
  reqNo: ''
});
let formRules = {
  visitTypeId: [{ required: true, message: '请选择病人来源' }]
};

function getSample(refSpecimenData: any) {
  Object.assign(form, refSpecimenData);
  findUnqSpecRecBySpecId();
}

// #region 【选择不合格标本原因】
let unqualifiedReasonGroupList = reactive([]);

// 根据mainInfoId获取列表
function findUnqSpecRecBySpecId() {
  unqApplicationRecList.length = 0;
  loading.value = true;
  const url = '/lab/req/unqualified/getUnqualified';
  const data = {
    rptGroup: form.rptGroup,
    sampleda: form.sampleda,
    sampleNo: form.sampleNo,
    reqNo: form.reqNo,
    sourceType: 'samp' //标识为校验报告来源
  };
  proxy.$http
    .get(url, { params: data })
    .then((res: any) => {
      unqApplicationRecList.push(...res.data);
      if (unqApplicationRecList.length > 0) {
        unqApplicationRecRowClick({ row: unqApplicationRecList[0], rowIndex: 0 });
        unqualifiedSpecRecRef.value.getDetil(unqApplicationRecList[0].id);
      }
      //子组件进行清空
      unqualifiedSpecRecRef.value.cleanData();
      loading.value = false;
    })
    .catch((err) => {
      loading.value = false;
      VxeMessage('数据请求异常', VxeNotifyType.error);
    });
}
//新增调用保存一条主记录
function saveUnqualified() {
  if (IsEmpty(form.id)) {
    VxeMessage('请选择一条上机记录', VxeNotifyType.error);
    return;
  }
  const params = {
    rptGroup: form.rptGroup,
    sampleda: form.sampleda,
    sampleNo: form.sampleNo,
    reqNo: form.reqNo,
    sourceType: 'samp' //标识为校验报告来源
  };
  const url = '/lab/req/unqualified/saveUnqualified';
  proxy.$http
    .post(url, params)
    .then((res: any) => {
      findUnqSpecRecBySpecId();
    })
    .catch((err: any) => {
      VxeMessage(err, VxeNotifyType.error);
    });
}

//保存
function save() {
  unqualifiedSpecRecRef.value.saveDetil();
}

//删除
function handleDelete(id) {
  proxy
    .$confirm('确认删除此项？', '提示', {
      cancelButtonClass: 'cancel-button',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    .then(() => {
      const url = '/lab/req/unqualified/deleteUnqualified';
      proxy.$http
        .get(url, { params: { id: id } })
        .then((res: any) => {
          findUnqSpecRecBySpecId();
          VxeMessage('删除成功', VxeNotifyType.success);
        })
        .catch((err) => {
          VxeMessage('删除失败', VxeNotifyType.error);
        });
    })
    .catch((err) => {
      console.log(err);
    });
}
// 行点击事件
function unqApplicationRecRowClick({ row, rowIndex }) {
  if (IsEmpty(row)) {
    saveUnqualified();
    return;
  }
  currentRowIndex = rowIndex;
  currentUnqSpecRec = row;
  (proxy.$refs.unqApplicationRecTable as any).setCurrentRow(row);
  unqualifiedSpecRecRef.value.getDetil(row.id);
}

//返回不合格原因中文字符串
function saveReasons(illustrate) {
  unqApplicationRecList[currentRowIndex].illustrate = illustrate;
}
//监听回车事件
function keyup(e: any) {
  if (e.$event.key == 'Enter') {
    addWklt();
  }
}

function addWklt() {
  //?条码号为空，不添加
  if (IsEmpty(form.reqNo)) {
    form.reqNo = '';
    (proxy.$refs.scanInputRef as any).focus();
    return;
  }
}

// #endregion
</script>
<style lang="scss" scoped>
#unqSpecRec {
  height: 100%;

  .area {
    width: 100%;
    height: 100%;
  }

  :deep(.el-descriptions .el-descriptions-item__label) {
    text-align: left;
  }
}
</style>
